package com.taobao.system.service.impl;

import com.taobao.basic.service.IBaseService;
import com.taobao.basic.service.impl.BaseServiceImpl;
import com.taobao.system.domain.SystemDictionaryDetail;
import com.taobao.system.domain.SystemDictionaryType;
import com.taobao.system.mapper.SystemDictionaryDetailMapper;
import com.taobao.system.mapper.SystemDictionaryTypeMapper;
import com.taobao.system.service.ISystemDictionaryTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SystemDictionaryTypeServiceImpl extends BaseServiceImpl<SystemDictionaryType> implements ISystemDictionaryTypeService {

   @Autowired
    private SystemDictionaryTypeMapper systemDictionaryTypeMapper;

    @Autowired
    private SystemDictionaryDetailMapper systemDictionaryDetailMapper;

    /**
     * 删除类型时删除掉明细
     * @param id
     */
    @Transactional
    @Override
    public void remove(Long id) {

        //获取明细里面types_id等于类型里面id的所有明细对象
        List<SystemDictionaryDetail> systemDictionaryDetail = systemDictionaryDetailMapper.findByTypeId(id);
        for (SystemDictionaryDetail dictionaryDetail : systemDictionaryDetail) {
            //获取对象id
            Long id1 = dictionaryDetail.getId();
            //循环删除
            systemDictionaryDetailMapper.remove(id1);
        }
        //删除字典明细之后再删除字典类型
        systemDictionaryTypeMapper.remove(id);
    }
}
